package com.wc.servlet; /**
 * @ClassName:${NAME}
 * @Author: 鹿建文
 * @Date: 2022/4/13  - 11:11
 * @Describe:
 */

import com.wc.dao.bean.PageBean;
import com.wc.domain.Admin;
import com.wc.domain.User;
import com.wc.service.AdminService;
import com.wc.service.UserService;
import com.wc.service.impl.AdminServiceImpl;
import com.wc.service.impl.UserServiceImpl;
import com.wc.vo.AdminVO;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/admin")
public class AdminServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        switch (op) {
            case "":
                break;
            default:
                break;

        }

    }




    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        switch (op) {
            case "login":
                login(request, response);
                break;
            case "updatePassword":
                updatePassword(request, response);
                break;

            default:
                break;

        }
    }



    private void updatePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        System.out.println(oldPassword + " " + newPassword);

        boolean res = false;
        try {
            res = adminService.updatePassword(1001, oldPassword, newPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (res) {
            //修改到成功,打开登录页面
            request.setAttribute("password", newPassword);
            request.getRequestDispatcher("login.ftl").forward(request, response);
        } else {
            //失败页面，到错误页面，错误页面3秒，打开修改密码页面
            request.setAttribute("msg", "密码修改失败，请在3s后重新修改密码！");
            request.setAttribute("aim", "update_password.html");
            request.getRequestDispatcher("error.ftl").forward(request, response);
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Admin admin = null;
        try {
            admin = adminService.login(username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // System.out.println(admin);
        if (admin != null) {
            //将admin转换成adminvo
            AdminVO adminVO = AdminVO.admin2AdminVO(admin);
            HttpSession session = request.getSession();
            //将管理员信息放到session中
            session.setAttribute("admin", adminVO);
//            getPage(request,response);
             response.sendRedirect("user?op=getPage");
            //将用户信息读出并保存到Session中

//            request.getRequestDispatcher("index.ftl").forward(request, response);
        } else {
            //失败页面，到错误页面，错误页面3秒，打开登录页面
            request.setAttribute("msg", "登录失败，请在3s后重新登录！");
            request.setAttribute("aim", "login.ftl");
            request.getRequestDispatcher("error.ftl").forward(request, response);
        }
    }
}